package cn.xspace.wnx.view.chat.chatrow;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.AppConstant;
import com.xspace.android.xafcommon.util.LogUtil;

import cn.xspace.chatui.view.chat.EaseChatRowVoicePlayClickListener;
import cn.xspace.wnx.R;
import cn.xspace.chatui.bean.ChatFileEntity;
import cn.xspace.chatui.bean.MsgRecordEntity;

public class EaseChatRowVoice extends EaseChatRowFile {

    private ImageView voiceImageView;
    private TextView voiceLengthView;
    private ImageView readStatusView;

    public EaseChatRowVoice(Context context, MsgRecordEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(isMySend() ?
                R.layout.chat_row_sent_voice : R.layout.chat_row_received_voice, this);
    }

    @Override
    protected void onFindViewById() {
        voiceImageView = ((ImageView) findViewById(R.id.iv_voice));
        voiceLengthView = (TextView) findViewById(R.id.tv_length);
        readStatusView = (ImageView) findViewById(R.id.iv_unread_voice);
    }

    @Override
    protected void onSetUpView() {
        int timeLength = message.mFileEntity.msg_dutime;
        if (timeLength > 0) {
            voiceLengthView.setText(timeLength + "\"");
            voiceLengthView.setVisibility(View.VISIBLE);
        } else {
            voiceLengthView.setVisibility(View.INVISIBLE);
        }
//        EMVoiceMessageBody voiceBody = (EMVoiceMessageBody) message.getBody();
//        int len = voiceBody.getLength();
//        if(len>0){
//            voiceLengthView.setText(voiceBody.getLength() + "\"");
//            voiceLengthView.setVisibility(View.VISIBLE);
//        }else{
//            voiceLengthView.setVisibility(View.INVISIBLE);
//        }
        if (EaseChatRowVoicePlayClickListener.playMsgId != null
                && EaseChatRowVoicePlayClickListener.playMsgId.equals(message.msg_id) && EaseChatRowVoicePlayClickListener.isPlaying) {
            //正在播放中
            AnimationDrawable voiceAnimation;
            if (!isMySend()) {
                //我接收的
                voiceImageView.setImageResource(R.drawable.voice_from_icon);
            } else {
                //我发送的
                voiceImageView.setImageResource(R.drawable.voice_to_icon);
            }
            voiceAnimation = (AnimationDrawable) voiceImageView.getDrawable();
            voiceAnimation.start();
        } else {
            //没有在播放中
            if (!isMySend()) {
                //我接收到的
                voiceImageView.setImageResource(R.drawable.ease_chatfrom_voice_playing);
            } else {
                //我发送的\
                voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing);
            }
        }

        if (!isMySend()) {
            LogUtil.d(TAG,"message.mFileEntity.isMediaPlayed>>>>>"+message.mFileEntity.isMediaPlayed);
            if (message.mFileEntity.isMediaPlayed == 1) {
                //播放过
                readStatusView.setVisibility(View.INVISIBLE);
            } else {
                readStatusView.setVisibility(View.VISIBLE);
            }
            if (message.mFileEntity.file_download_status == ChatFileEntity.DownloadStatus.DOWNLOAD_COMPLETE.toNumber()) {
                progressBar.setVisibility(View.INVISIBLE);
            } else if (message.mFileEntity.file_download_status == ChatFileEntity.DownloadStatus.DOWNLOAD_ING.toNumber() ||
                    message.mFileEntity.file_download_status == ChatFileEntity.DownloadStatus.DOWNLOAD_NOT.toNumber()) {
                progressBar.setVisibility(View.VISIBLE);
                //还没有下载等待下载
                getFileByHttp();
            }
            return;
        }
//
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
//            if (message.isListened()) {
//                // hide the unread icon
//                readStatusView.setVisibility(View.INVISIBLE);
//            } else {
//                readStatusView.setVisibility(View.VISIBLE);
//            }
//            EMLog.d(TAG, "it is receive msg");
//            if (voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
//                    voiceBody.downloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) {
//                progressBar.setVisibility(View.VISIBLE);
//                setMessageReceiveCallback();
//            } else {
//                progressBar.setVisibility(View.INVISIBLE);
//
//            }
//            return;
//        }

        // until here, handle sending voice message
        handleSendMessage();
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void buildSendMap() {
        super.buildSendMap();
        sendMap.put("msg_dutime", message.mFileEntity.msg_dutime);
    }

    @Override
    protected String getSaveDir() {
        return AppConstant.VOICE_ADDRESS_DL;
    }

    @Override
    protected void onBubbleClick() {
        new EaseChatRowVoicePlayClickListener(message, voiceImageView, readStatusView, adapter, activity).onClick(bubbleLayout);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (EaseChatRowVoicePlayClickListener.currentPlayListener != null && EaseChatRowVoicePlayClickListener.isPlaying) {
            EaseChatRowVoicePlayClickListener.currentPlayListener.stopPlayVoice();
        }
    }

//    private void getVoiceByHttp() {
//        OkHttpManager.getInstance().downloadAsync(message.mFileEntity.file_url_remote, AppConstant.VOICE_ADDRESS_DL, new OkHttpManager.DataCallBack() {
//            @Override
//            public void requestBefore() {
//            }
//
//            @Override
//            public void requestFailure(Request request, IOException e) {
//                progressBar.setVisibility(View.INVISIBLE);
////                imageView.setImageResource(R.drawable.chat_default_image_error);
//                ChatFileDao.getInstance().updateFileDownLoadStausByTime(message.inserttime, ChatFileEntity.DownloadStatus.DOWNLOAD_FIALED.toNumber());
//            }
//
//            @Override
//            public void requestSuccess(BaseReturnBean result) throws Exception {
//            }
//
//            @Override
//            public void requestSuccess(String result) throws Exception {
//                progressBar.setVisibility(View.INVISIBLE);
////                            imageView.setImageResource();
//                LogUtil.d(TAG, "本地下载路径为" + result);
//                message.mFileEntity.file_url_local = result;
//                message.mFileEntity.file_download_status = ChatFileEntity.DownloadStatus.DOWNLOAD_COMPLETE.toNumber();
//                //更新数据库状态
//                ChatFileDao.getInstance().updateFileDownLoadStausByTime(message.inserttime, ChatFileEntity.DownloadStatus.DOWNLOAD_COMPLETE.toNumber());
//                ChatFileDao.getInstance().updateFileLocalPath(message.inserttime, result);
////                showImageView(result, imageView, message.mFileEntity.file_url_remote);
//            }
//        });
//    }

}
